//
//  YMAutoGenerateModelTool.m
//  PinGJByOwnerSide
//
//  Created by lantaiyuan on 2017/11/15.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

#import "YMAutoGenerateModelTool.h"

@implementation YMAutoGenerateModelTool
+ (void)propertyWithDictionary:(NSDictionary *)dict{
    NSMutableString * mutableStr = [NSMutableString string];
    
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSString * str;
        if ([obj isKindOfClass:NSClassFromString(@"NSTaggedPointerString")]||
            [obj isKindOfClass:NSClassFromString(@"__NSCFString")]||
            [obj isKindOfClass:NSClassFromString(@"__NSCFConstantString")]) {
            str = [NSString stringWithFormat:@"@property(nonatomic, copy) NSString *%@;",key];
        }
        
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFNumber")]) {
            str = [NSString stringWithFormat:@"@property(nonatomic, strong) NSNumber *%@;",key];
            NSString * tempStr = [NSString stringWithFormat:@"%@", obj];
            if ([self isPureInt:tempStr]) {
                str = [NSString stringWithFormat:@"@property(nonatomic, assign) NSInteger %@;",key];
            }else if ([self isPureFloat:tempStr]) {
                str = [NSString stringWithFormat:@"@property(nonatomic, assign) CGFloat %@;",key];
            }
        }
        
        if ([obj isKindOfClass:NSClassFromString(@"__NSDictionaryI")]||
            [obj isKindOfClass:NSClassFromString(@"__NSCFDictionary")]) {
            str = [NSString stringWithFormat:@"@property(nonatomic, strong) NSDictionary *%@;",key];
        }
        
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFArray")]) {
            str = [NSString stringWithFormat:@"@property(nonatomic, strong) NSDictionary *%@;",key];
        }
        
        if ([obj isKindOfClass:NSClassFromString(@"__NSCFBoolean")]) {
            str = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
        }
        
        [mutableStr appendString:str];
        [mutableStr appendString:@"\n"];
    }];
    
    NSLog(@"%@",mutableStr);
}

//判断字符串是否是整型
+ (BOOL)isPureInt:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return[scan scanInt:&val] && [scan isAtEnd];
    
}

//判断是否为浮点形：
+ (BOOL)isPureFloat:(NSString*)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    float val;
    
    return[scan scanFloat:&val] && [scan isAtEnd];
    
}
@end
